﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Entity;
using BAL;
using Common;

namespace Web
{
    public partial class AddQuestion : System.Web.UI.Page
    {
        private IList<DifficInfo> Difficinfos = new List<DifficInfo>();
        private IList<QuesTypeInfo> QuesTypeInfos = new List<QuesTypeInfo>();
        private IList<LevelInfo> LevelInfos = new List<LevelInfo>();
        private IList<CognitiveLevelInfo> CognitiveLevelInfos = new List<CognitiveLevelInfo>();
        private IList<BookInfo> BookInfos = new List<BookInfo>();
        private UserLinkExtendInfo uinfo = new UserLinkExtendInfo();
        private int delid = 0, editid = 0, _count = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!UserValidate.TeacherCheckLogin(this.Request))
            {
                Response.Write("<script type=\"text/javascript\">top.location.href='login.aspx';</script>");
                Response.Redirect("login.aspx");
                Response.End();
            }

            if (Request.QueryString["delid"] != null && Request.QueryString["delid"] != string.Empty)
            {
                if (PageValidate.IsNumber(Request.QueryString["delid"]))
                {
                    delid = int.Parse(Request.QueryString["delid"]);
                }
            }
            if (Request.QueryString["editid"] != null && Request.QueryString["editid"] != string.Empty)
            {
                if (PageValidate.IsNumber(Request.QueryString["editid"]))
                {
                    editid = int.Parse(Request.QueryString["editid"]);
                }
            }
            if (!IsPostBack)
            {
                LoadDiffic();
                LoadQuesType();
                LoadSubject();
                LoadCognitiveLevel();
                if (delid > 0)
                {
                    QuestionInfo qinfo = new QuestionInfo();
                    qinfo = Question.GetQuestionInfoById(delid);
                    if (qinfo.StatusId !=10)
                    {
                        Utility.ShowJsAlertAndRedirect("MyQuestion.aspx", "您只能删除暂存题目", this);
                        return;
                    }
                    Question.DelQuestion(delid);//删除操作
                    Response.Redirect("MyQuestion.aspx");
                }
                if (editid > 0)
                {
                    QuestionInfo questionInfo = new QuestionInfo();
                    questionInfo = Question.GetQuestionInfoById(editid);
                    if (questionInfo.StatusId != 10)
                    {
                        Utility.ShowJsAlertAndRedirect("MyQuestion.aspx", "您只能编辑暂存题目", this);
                        return;
                    }
                    else
                    {
                        /*QuesTypeIdList.SelectedValue = questionInfo.QuesTypeId.ToString();//试题类型
                        DifficIdList.SelectedValue = questionInfo.DifficId.ToString();//难度级别
                        //AssessIdList.SelectedValue = questionInfo.AssessId.ToString();
                        KnowIdList.SelectedValue= questionInfo.KnowId.ToString();
                        SubKnowLable.Text  = Knowledge.GetKnowledgeInfoById(questionInfo.KnowId).Name;
                        Score.Text = questionInfo.Score.ToString();
                        KeyWord.Text = questionInfo.KeyWord;
                        Title.Value = questionInfo.Title;
                        Answer1.Value = questionInfo.Answer;
                        _count = questionInfo.Counts;*/
                       // QuestionInfo questionInfo = new QuestionInfo();
                        //questionInfo = Question.GetQuestionInfoById(editid);
                       // QuesTypeId.Text = QuesType.GetQuesTypeInfoById(questionInfo.QuesTypeId).Keyword;
                        //DifficId.Text = Diffic.GetDifficInfoById(questionInfo.DifficId).Keyword;
                       // LevelId.Text = questionInfo.LevelId.ToString();
                        QuesTypeId.Text = QuesType.GetQuesTypeInfoById(questionInfo.QuesTypeId).Keyword;
                        DifficId.Text = Diffic.GetDifficInfoById(questionInfo.DifficId).Keyword;
                        SubjectId.Text = Subject.GetSubjectInfoById(questionInfo.SubjectId).Keyword;
                        CognitiveLevelId.Text = CognitiveLevel.GetSingleInfo(questionInfo.CognitiveLevelId).Keyword;
                        SetSubjectId(questionInfo.SubjectId);
                        CheckBoxListHelper.SetSelectedValue(SubKnowsList, questionInfo.SubKnow);
                        CheckBoxListHelper.SetSelectedValue(ROCList, questionInfo.ROC);
                        Score.Text = questionInfo.Score.ToString();//分数
                        KeyWord.Text = questionInfo.KeyWord;//关键字
                        ComputeTime.Text = questionInfo.ComputeTime.ToString();
                        Title.Value = questionInfo.Title.Replace("\"upfile/", "\"../../upfile/");//题目
                        Answer1.Value = questionInfo.Answer;//答案
                        _count = questionInfo.Counts;
                        KnowId.Text = Knowledge.GetKnowledgeInfoById(questionInfo.KnowId).Keyword;

                        //SubjectIdList.SelectedValue = questionInfo.SubjectId.ToString();//选择课程
                        //this.CognitiveLevelIdList.SelectedValue = questionInfo.CognitiveLevelId.ToString();//认知层
                        //SetSubjectId(questionInfo.SubjectId);//通过科目设置知识点
                        //KnowIdList.SelectedValue= questionInfo.KnowId.ToString();//知识树
                        //QuesTypeIdList.SelectedValue = questionInfo.QuesTypeId.ToString();//试题类型
                        //DifficIdList.SelectedValue = questionInfo.DifficId.ToString();//难度级别
                        //Score.Text = questionInfo.Score.ToString();//分数
                        //KeyWord.Text = questionInfo.KeyWord;//关键字
                        //ComputeTime.Text = questionInfo.ComputeTime.ToString();
                        //CheckBoxListHelper.SetSelectedValue(SubKnows, questionInfo.SubKnow);
                        //CheckBoxListHelper.SetSelectedValue(ROC, questionInfo.ROC);
                        //Title.Value = questionInfo.Title.Replace("\"upfile/", "\"../../upfile/");//题目
                        //Answer1.Value = questionInfo.Answer;//答案
                        //_count = questionInfo.Counts;
                    }
                }


            }
            if (Request.QueryString["post"] != null)
            {
                Utility.ShowBoxyAlert("添加成功，请继续",this);
            }
        }

        protected void LoadSubject()
        {
            SubjectIdList.Items.Clear();
            IList<SubjectInfo> Subject_Infos = new List<SubjectInfo>();
            SubjectIdList.Items.Add(new ListItem("请选择课程", "0"));
            Subject_Infos = Subject.GetSubjectInfos();
            foreach (SubjectInfo info in Subject_Infos)
            {
                SubjectIdList.Items.Add(new ListItem(info.Name, info.Keyword));//+"    "+info.Keyword
            }
        }

        protected void LoadDiffic()
        {
            Difficinfos = Diffic.GetDifficInfos(0, 0);
            DifficIdList.Items.Clear();
            DifficIdList.Items.Add(new ListItem("请选择难度级别", ""));
            foreach (DifficInfo info in Difficinfos)
            {
                DifficIdList.Items.Add(new ListItem(info.Name + "    " + info.Keyword.ToString(), info.Keyword));
            }
        }

        protected void LoadQuesType()
        {
            QuesTypeInfos = QuesType.GetQuesTypeInfos(0, 0);
            QuesTypeIdList.Items.Clear();
            QuesTypeIdList.Items.Add(new ListItem("请选择试题类型", ""));
            foreach (QuesTypeInfo info in QuesTypeInfos)
            {
                QuesTypeIdList.Items.Add(new ListItem(info.Name + "    " + info.Keyword.ToString(), info.Keyword));
            }
        }

        protected void LoadBook(int inputSubjectId)
        {
            BookInfos = Book.GetBookInfosBySubject(inputSubjectId, 0, 0);
            ROCList.Items.Clear();
            foreach (BookInfo info in BookInfos)
            {
                ROCList.Items.Add(new ListItem(info.Name.ToString() , info.Id.ToString()));//+ "    " + info.Keyword.ToString()
            }
        }

        protected void LoadCognitiveLevel()
        {
            CognitiveLevelInfos = CognitiveLevel.GetList(0);
            CognitiveLevelIdList.Items.Clear();
            CognitiveLevelIdList.Items.Add(new ListItem("请选择认知层次", ""));
            foreach (CognitiveLevelInfo info in CognitiveLevelInfos)
            {
                CognitiveLevelIdList.Items.Add(new ListItem(info.Name + "    " + info.Keyword.ToString(), info.Keyword));
            }
        }

        protected void LoadKnowledge(int ParentID, int inputSubjectId, string line)
        {
            if (ParentID == 0)
            {
                KnowIdList.Items.Clear();
                SubKnowsList.Items.Clear();
                KnowIdList.Items.Add(new ListItem("请选择知识点", "0"));
                IList<KnowledgeInfo> knowledgeInfos = Knowledge.GetRootKnowledgeInfo(0, inputSubjectId);
                foreach (KnowledgeInfo info in knowledgeInfos)
                {
                    ListItem item = new ListItem(info.Keyword+"  "+info.Name , info.Keyword);//+ "    " + info.Keyword.ToString()
                    KnowIdList.Items.Add(item);
                    item = new ListItem(info.Keyword, info.Keyword);
                    SubKnowsList.Items.Add(item);
                    LoadKnowledge(info.Id, inputSubjectId, "|——");
                }
            }
            else
            {
                IList<KnowledgeInfo> knowledgeInfos = Knowledge.GetKnowledgeInfos(ParentID);
                foreach (KnowledgeInfo info in knowledgeInfos)
                {
                    ListItem item = new ListItem(line +info.Keyword+"  "+ info.Name, info.Keyword);//+ "    " + info.Keyword.ToString()
                    KnowIdList.Items.Add(item);
                    item = new ListItem(line + info.Keyword, info.Keyword);
                    SubKnowsList.Items.Add(item);
                    LoadKnowledge(info.Id, inputSubjectId, "|——" + line);
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            PublicSave( 4);
        }

        protected void SetSubjectId(int inputSubjectId)
        {
            LoadKnowledge(0, inputSubjectId, "——");
            LoadBook(inputSubjectId);
            //KnowIdList.Text = "";
            ROCLabel.Visible = true;
            KnowIdList.Visible = true;
            SubKnowLabel.Visible = true;
        }

        protected void SubjectIdList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (SubjectIdList.SelectedIndex > -1)
            {
                int tSubjectId = Subject.GetSubjectIdByKeyword(SubjectIdList.SelectedValue.ToString());//通过关键字获取科目ID
                //int tSubjectId = int.Parse(SubjectIdList.SelectedValue.Trim());
                SetSubjectId(tSubjectId);
            }
        }

        protected void btnZhanCun_Click(object sender, EventArgs e)
        {
            PublicSave(10);
        }
        public  void PublicSave(int statu){
            string _ROC = null, _Printer = null, _Title = null, _QuesTypeKeyword = null, _DifficKeyword = null, _KnowKeyword = null, _SubjectKeyword = null, _BookKeyword = null;
            string _KeyWord = KeyWord.Text.Trim();//关键字
            string _SubKnow = CheckBoxListHelper.SelectedValue(SubKnowsList);//知识点
            decimal _Score = 0;
            int _LevelId = 0, _QuesTypeId = 0, _DifficId = 0, _KnowId = 0, _CognitiveLevelId = 0, _ComputeTime = 0, _SubjectId = 0;
            DateTime _PrintDate = DateTime.Today;
            
            if (ROCList.SelectedValue.Trim() == "")
            {
                Utility.ShowBoxyAlert("必须选择适用范围", this);
                return;
            }
            else
            {
                _ROC = CheckBoxListHelper.SelectedValue(ROCList);
            }
            QuesTypeInfo qtinfo = new QuesTypeInfo();
            if (QuesTypeId.Text.Trim() == "")
            {
                Utility.ShowBoxyAlert("必须设定题目类型", this);
                return;
            }
            else if (!QuesType.CheckQuesTypeExistByKeyword(QuesTypeId.Text.Trim(), 0))
            {
                Utility.ShowBoxyAlert("题目类型编码错误，请核实", this);
                return;
            }
            else
            {
                _QuesTypeKeyword = QuesTypeId.Text.Trim();
                _QuesTypeId = QuesType.GetQuesTypeIdByKeyword(_QuesTypeKeyword);
                qtinfo = QuesType.GetQuesTypeInfoById(_QuesTypeId);
            }

            if (SubjectId.Text.Trim() == "")
            {
                Utility.ShowBoxyAlert("请选择科目", this);
                return;
            }
            else if (!Subject.CheckSubjectExistByKeyword(SubjectId.Text.Trim(), 0))
            {
                Utility.ShowBoxyAlert("科目编码错误，请核实", this);
                return;
            }
            else
            {
                _SubjectKeyword = SubjectId.Text.Trim();
                _SubjectId = Subject.GetSubjectIdByKeyword(SubjectId.Text.Trim());
            }

            if (DifficId.Text.Trim() == "")
            {
                Utility.ShowBoxyAlert("必须设定题目难度", this);
                return;
            }
            else if (!Diffic.CheckDifficExistByKeyword(DifficId.Text.Trim(), 0))
            {
                Utility.ShowBoxyAlert("题目难度编码错误，请核实", this);
                return;
            }
            else
            {
                _DifficId = Diffic.GetDifficIDByKeyword(DifficId.Text.Trim());
            }

            if (CognitiveLevelId.Text.Trim() == "")
            {
                Utility.ShowBoxyAlert("必须设定认知层次", this);
                return;
            }
            else if (!CognitiveLevel.CheckCognitiveLevelExistByKeyword(CognitiveLevelId.Text.Trim(), 0))
            {
                Utility.ShowBoxyAlert("题目认知层次编码错误，请核实", this);
                return;
            }
            else
            {
                _CognitiveLevelId = CognitiveLevel.GetCognitiveLevelIDByKeyword(CognitiveLevelId.Text.Trim());
            }
            
            if (KnowId.Text.Trim() == "")
            {
                Utility.ShowBoxyAlert("必须选择知识点", this);
                return;
            }
            else if (!Knowledge.CheckKnowledgeExistByKeyword(KnowId.Text.Trim(), 0,_SubjectId))
            {
                Utility.ShowBoxyAlert("知识点编码错误，请核实", this);
                return;
            }
            else
            {
                _KnowKeyword = KnowId.Text.Trim();
                _KnowId = Knowledge.GetKnowledgeIDByKeyword(KnowId.Text.Trim(),_SubjectId);
            }

            if (Score.Text.Trim() == "")
            {
                Utility.ShowBoxyAlert("必须设定题目分数", this);
                return;
            }
            else //对字符的处理
            {
                try
                {
                    Convert.ToSingle(Score.Text.Trim());
                }
                catch (Exception ex)
                {
                    Utility.ShowBoxyAlert("分值必须为数字类型", this);
                    return;
                }
            }

            if (Convert.ToSingle(Score.Text.Trim()) < 0.5 || Convert.ToSingle(Score.Text.Trim()) > 60)
            {
                Utility.ShowBoxyAlert("题目的分数在(0.5~60)范围内", this);
                return;
            }
            else if (Score.Text.Trim().LastIndexOf('.') != -1)//数字为小数
            {
                if (Convert.ToInt32(Score.Text.Trim().Substring(Score.Text.Trim().LastIndexOf('.') + 1, 1)) != 5 && Convert.ToInt32(Score.Text.Trim().Substring(Score.Text.Trim().LastIndexOf('.') + 1, 1))!=0)
                {
                    Utility.ShowBoxyAlert("题目分值小数点后必须是5", this);
                    return;
                }
            }
            _Score = decimal.Parse(Score.Text.Trim());//给分数变量赋值

            if (Title.Value.Trim().Length < 20)
            {
                Utility.ShowBoxyAlert("题目内容过短", this);
                return;
            }
            else
            {
                _Title = Title.Value.Trim().Replace("\"../../upfile/", "\"upfile/");
            }

            if (ComputeTime.Text.Trim() == "")
            {
                Utility.ShowBoxyAlert("必须设定估计时间", this);
                return;
            }
            else //对输入字符的处理
            {
                try
                {
                    Convert.ToInt32(ComputeTime.Text.Trim());
                }
                catch (Exception ex)
                {
                    Utility.ShowBoxyAlert("时间必须为数字类型",this);
                    return;
                }
            }
            
            if (Convert.ToSingle(ComputeTime.Text.Trim()) < 1 || Convert.ToSingle(ComputeTime.Text.Trim()) > 5400)
            {
                Utility.ShowBoxyAlert("答题时间必需在(1~5400)范围内", this);
                return;
            }
            else
            {
                _ComputeTime = int.Parse(ComputeTime.Text.Trim());
            }

            string _Answer = "";
            if (qtinfo.Name != "写作题")
            {
                if (PageValidate.IsEmpty(Answer1.Value))
                {
                    Utility.ShowBoxyAlert("请设定参考答案", this);
                    return;
                }
                else
                {
                    _Answer = Answer1.Value.Trim();//答案
                }
            }
            else
            {
                _Answer = Answer1.Value.Trim();//答案
            }

            //KnowledgeInfo k1 = Knowledge.GetKnowledgeInfoById(_KnowId);
            //_SubjectKeyword = Subject.GetSubjectInfoById(Convert.ToInt32(k1.Subject)).Keyword;
            //_BookKeyword = Book.GetBookInfoById(Convert.ToInt32(k1.Book)).Keyword;
            QuestionInfo q1 = new QuestionInfo();
            if (editid > 0)
            {
                q1 = Question.GetQuestionInfoById(editid);
            }
            q1.Answer = _Answer;
            q1.AssessId = 0;
            q1.BookId = 0;
            q1.DifficId = _DifficId;
            q1.KeyWord = _KeyWord;
            q1.KnowId = _KnowId;
            q1.LevelId = 13;
            q1.PrintDate = DateTime.Now;
            uinfo = UserValidate.TeacherGetInfo(this.Request);
            q1.Printer = uinfo.Name;
            q1.PrinterId = uinfo.Id;
            q1.QuesTypeId = _QuesTypeId;
            q1.Score = _Score;
            q1.State = (Byte)statu;
            q1.StatusId = statu;
            q1.SubjectId = _SubjectId;
            q1.CognitiveLevelId = _CognitiveLevelId;
            q1.ComputeTime = _ComputeTime;
            q1.SubKnow = _SubKnow;
            q1.ROC = _ROC;
            q1.Title = _Title;
            if (editid == 0)
            {
                //int count = Question.GetQuestionMax(new QuestionInfo { QuesTypeId = _QuesTypeId, KnowId = _KnowId }) + 1;
                q1.Serial = "no";
                q1.Counts = 0;
                q1.UserId = uinfo.Id;
                q1.Remark = "";
                q1.CreateDate = DateTime.Now;
                Question.AddQuestion(q1);
                Utility.ShowBoxyConfirm("添加成功，是否清除已有选项？", "AddQuestion.aspx", this);
                Title.Value = "";
                Answer1.Value = "";
                KeyWord.Text = "";
                //KnowIdList.Value = _KnowId.ToString();
                //KnowIddis.Text = Knowledge.GetKnowledgeInfoById(_KnowId).Name;
                return;
            }
            else
            {
                q1.Id = editid;
                q1.Serial = "no";
                Question.EditQuestion(q1);
                Response.Redirect("MyQuestion.aspx");
            }
        }
    }
}
